﻿using BaoShouTangServer.ApiServies;
using BaoShouTangServer.DTOModel;
using BaoShouTangServer.Models;
using BaoShouTangServer.Service;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using U8Login;

namespace BaoShouTangServer.Controllers
{
    public class SOMainController : Controller
    {
        private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        public JsonResult Add241([FromBody] DtoSOMain dtoSOMain)
               //public JsonResult Add(string dtoSOMain)
        {
            Logger.Warn("Add241接受到的销售订单：" + JsonConvert.SerializeObject(dtoSOMain));

            string resp = "";
            string msg = "";
            string responseMsg = "";
            HttpData<object> httpData = new HttpData<object>();

                string 到货单接口返回result = "";
            string Newid = "";

            clsLogin clsLogin = LogInService.GetLogin(ref resp, ref msg, "241");
                if (resp != "登陆成功")
                {
                    return Json(new { success = false, message = $"{msg}" });
                }
            //生成到货单
            string isSuccess = SO_SOMainService.生成销售订单("241",clsLogin, dtoSOMain,ref Newid, ref 到货单接口返回result);
         
            
            if (string.IsNullOrEmpty(isSuccess) == false)
            {

                httpData.msg += $"Add241推送的guid:{dtoSOMain.cdefine1}生成销售单失败,失败原因:{isSuccess},{到货单接口返回result}。\r\n";
                Logger.Info(httpData.msg);
            }
            else
            { 
                //跟新数据库
                int.TryParse(Newid, out int id);
                SaleOrderQ saleOrderQ = SqlSugarHelp.scope.GetConnection("241").Queryable<SaleOrderQ>().Where(it => it.id == id).First();
                
                httpData.msg += $"Add241推送的guid:{dtoSOMain.cdefine1}生成销售单成功,单号为:{saleOrderQ?.csocode},id:{Newid}。\r\n";
                httpData.data = saleOrderQ;
                Logger.Warn("Add241生成U8的的销售订单：" + responseMsg);
            }
            //httpData.msg = responseMsg;
            return Json(httpData);
        }


        public JsonResult Add242([FromBody] DtoSOMain dtoSOMain)
        //public JsonResult Add(string dtoSOMain)
        {
            Logger.Warn("Add242接受到的销售订单：" + JsonConvert.SerializeObject(dtoSOMain));

            string resp = "";
            string msg = "";
            string responseMsg = "";
            HttpData<object> httpData = new HttpData<object>();

            string 到货单接口返回result = "";
            string Newid = "";

            clsLogin clsLogin = LogInService.GetLogin(ref resp, ref msg, "242");
            if (resp != "登陆成功")
            {
                return Json(new { success = false, message = $"{msg}" });
            }
            //生成到货单
            string isSuccess = SO_SOMainService.生成销售订单("242",clsLogin, dtoSOMain, ref Newid, ref 到货单接口返回result);


            if (string.IsNullOrEmpty(isSuccess) == false)
            {

                httpData.msg += $"Add242推送的guid:{dtoSOMain.cdefine1}生成销售单失败,失败原因:{isSuccess},{到货单接口返回result}。\r\n";
                Logger.Info(httpData.msg);
            }
            else
            {
                //跟新数据库
                int.TryParse(Newid, out int id);
                SaleOrderQ saleOrderQ = SqlSugarHelp.scope.GetConnection("242").Queryable<SaleOrderQ>().Where(it => it.id == id).First();

                httpData.msg += $"Add242推送的guid:{dtoSOMain.cdefine1}生成销售单成功,单号为:{saleOrderQ?.csocode},id:{Newid}。\r\n";
                httpData.data = saleOrderQ;
                Logger.Warn("Add242生成U8的的销售订单：" + responseMsg);
            }
            //httpData.msg = responseMsg;
            return Json(httpData);
        }

    }
}